haproxy 源码编译安装

下载

1
2
3
4
5
6
7
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.25.tar.gz
[root@localhost src]# yum -y install cmake gcc gcc-c++ autoconf automake zlib* libxml* \
ncurses ncurses-devel libtool-ltdl-devel* make bison bison-devel libaio
[root@localhost src]# tar zxvf haproxy-1.4.25.tar.gz
[root@localhost src]# cd haproxy-1.4.25
[root@localhost haproxy-1.4.25]#

开始编译安装

        MAKE 参数参考文件中的 README 文件

1
2
3
4
5
6
7
8
9
10
11
[root@localhost haproxy-1.4.25]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[root@localhost haproxy-1.4.25]# make install PREFIX=/usr/local/haproxy
[root@localhost haproxy-1.4.25]# install -d /usr/local/sbin
[root@localhost haproxy-1.4.25]# install haproxy /usr/local/sbin
[root@localhost haproxy-1.4.25]# install -d /usr/local/share/man/man1
[root@localhost haproxy-1.4.25]# install -m 644 doc/haproxy.1 /usr/local/share/man/man1
[root@localhost haproxy-1.4.25]# install -d /usr/local/doc/haproxy
[root@localhost haproxy-1.4.25]# for x in configuration architecture haproxy-en haproxy-fr; do \
> install -m 644 doc/$x.txt /usr/local/doc/haproxy ; \
> done
[root@localhost haproxy-1.4.25]#

        安装完成,检测是否安装成功

1
2
3
[root@localhost haproxy-1.4.25]# haproxy -v
HA-Proxy version 1.4.25 2014/03/27
Copyright 2000-2014 Willy Tarreau <w@1wt.eu>

        则安装成功

配置 haproxy.cfg 文件

        用的 /root/haproxy-1.4.25/examples/haproxy.cfg 这个是自带的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
[root@localhost examples]# vim haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
# chroot /usr/share/haproxy
chroot /usr/local/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
#============================这一段是后面加上去的。 就是WEB代理
listen web_proxy
bind *:80
mode http
option httpchk GET /index.html
server s1 192.168.11.210:80 weight 3 check
server s2 192.168.11.211:80 weight 3 check
#后面的我做的注释
#listen appli1-rewrite 0.0.0.0:10001
# cookie SERVERID rewrite
# balance roundrobin
# server app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
# server app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
# server app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5
# server app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5
#
#listen appli2-insert 0.0.0.0:10002
# option httpchk
# balance roundrobin
# cookie SERVERID insert indirect nocache
# server inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
# server inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
# capture cookie vgnvisitor= len 32
#
# option httpclose # disable keep-alive
# rspidel ^Set-cookie:\ IP= # do not let this cookie tell our internal IP address
#
#listen appli3-relais 0.0.0.0:10003
# dispatch 192.168.135.17:80
#
#listen appli4-backup 0.0.0.0:10004
# option httpchk /index.html
# option persist
# balance roundrobin
# server inst1 192.168.114.56:80 check inter 2000 fall 3
# server inst2 192.168.114.56:81 check inter 2000 fall 3 backup
#
#listen ssl-relay 0.0.0.0:8443
# option ssl-hello-chk
# balance source
# server inst1 192.168.110.56:443 check inter 2000 fall 3
# server inst2 192.168.110.57:443 check inter 2000 fall 3
# server back1 192.168.120.58:443 backup
#listen appli5-backup 0.0.0.0:10005
# option httpchk *
# balance roundrobin
# cookie SERVERID insert indirect nocache
# server inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
# server inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
# server inst3 192.168.114.57:80 backup check inter 2000 fall 3
# capture cookie ASPSESSION len 32
# srvtimeout 20000
# option httpclose # disable keep-alive
# option checkcache # block response if set-cookie & cacheable
# rspidel ^Set-cookie:\ IP= # do not let this cookie tell our internal IP address
# errorloc 502 http://192.168.114.58/error502.html
# errorfile 503 /etc/haproxy/errors/503.http

启动服务

1
2
3
[root@localhost examples]# haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg
[WARNING] 316/220055 (2376) : parsing [/root/haproxy-1.4.25/examples/haproxy.cfg:22]: keyword 'redispatch' is deprecated, please use 'option redispatch' instead.
[ALERT] 316/220055 (2376) : [haproxy.main()] Cannot chroot(/usr/local/sbin/haproxy).

        出现的报错信息,下面我们来解决问题。因为上面配置文件是我改后的,应该没有什么问题。
        [ALERT] 316/220055 (2376) : [haproxy.main()] Cannot chroot(/usr/local/sbin/haproxy).

        是配置文件chroot的目录不对,根据我的文档安装改成 chroot /urs/local/haproxy 就可以解决了 执行 mkdir /usr/share/haproxy

        [WARNING] 316/220055 (2376) : parsing [/root/haproxy-1.4.25/examples/haproxy.cfg:22]: keyword 'redispatch' is deprecated, please use 'option redispatch' instead.

        这个报错信息是配置文件22行的问题, 我是做了注释掉就可以起动服务了。 注释到相对应的错误行

        再次起动服务:

1
2
[root@localhost examples]# haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg
[root@localhost examples]#

        没有任何提示,那我们检测一下是否启动成功

        看一下进程信息

1
2
3
[root@localhost examples]# ps -ef |grep haproxy
nobody 3826 1 0 22:38 ? 00:00:00 haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg
root 3837 1260 0 22:38 pts/0 00:00:00 grep haproxy

        进程已经在

        看一下端口信息

1
2
3
[root@localhost examples]# netstat -anp |grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3826/haproxy
udp 0 0 0.0.0.0:43549 0.0.0.0:* 3826/haproxy

        把配置文件移动到/etc目录下

1
2
[root@localhost examples]# cd /etc/
[root@localhost etc]#

        复制haproxy配置文件到/etc/目录下面

1
[root@localhost etc]# cp /root/haproxy-1.4.25/examples/haproxy.cfg haproxy.cfg

        再一次查看进程 kill掉。 在用/etc/haproxy.cfg的配置文件启动看看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost etc]# ps -ef | grep haproxy
nobody 3826 1 0 22:38 ? 00:00:00 haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg
root 3901 1260 0 22:40 pts/0 00:00:00 grep haproxy
[root@localhost etc]# kill 3826
[root@localhost etc]# ps -ef | grep haproxy
root 3928 1260 0 22:41 pts/0 00:00:00 grep haproxy
```
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;已经kill掉进程,进程已经没有在运行了, 我们现在用/etc/haproxy.cfg 来运行服务
```bash
[root@localhost etc]# haproxy -f /etc/haproxy.cfg
[root@localhost etc]# ps -ef |grep haproxy.cfg
nobody 3959 1 0 22:42 ? 00:00:00 haproxy -f /etc/haproxy.cfg
root 3965 1260 0 22:43 pts/0 00:00:00 grep haproxy.cfg

        已经看到运行成功

设置以服务形式启动

        目录切换到/etc/init.d

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
[root@localhost etc]# cd /etc/init.d/
[root@localhost init.d]# vi haproxy
#!/bin/bash
#
# haproxy
#
# chkconfig: 35 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
config="/etc/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $config
}
start() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $config -p /var/run/$prog.pid
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
checkconfig)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
exit 2
esac

        重起服务haproxy

1
2
[root@localhost init.d]# service haproxy restart
env: /etc/init.d/haproxy: Permission denied

        这个提示。大概是权限问题。下面加上执行权限

1
2
3
4
5
[root@localhost init.d]# ll haproxy
-rw-r--r--. 1 root root 2588 Nov 13 23:02 haproxy
[root@localhost init.d]# chmod -R 655 haproxy
[root@localhost init.d]# ll haproxy
-rw-r-xr-x. 1 root root 2588 Nov 13 23:02 haproxy

        看一下进程是否存在,不存在重起haproxy服务

[root@localhost init.d]# ps -ef | grep haproxy  
root      7409  1260  0 23:56 pts/0    00:00:00 grep haproxy  
[root@localhost init.d]# service haproxy restart  
Stopping haproxy:                                          [FAILED]  
Starting haproxy:                                          [  OK  ]  
[root@localhost init.d]# ps -ef | grep haproxy  
nobody    7444     1  0 23:56 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -D -f /etc/haproxy.cfg -p /var/run/haproxy.pid  
root      7449  1260  0 23:56 pts/0    00:00:00 grep haproxy  
[root@localhost init.d]#

        配置上面就完成了